package com.shareach.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BankCollectDao;
import com.shareach.domain.BankCollectDomain;
import com.shareach.service.BankCollectService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

@Service
public class BankCollectServiceImpl extends BaseService<BankCollectDomain> implements BankCollectService {

	@Autowired
	private BankCollectDao bankCollectDao;

	@Override
	public synchronized void save(BankCollectDomain domain) {

		bankCollectDao.insert(domain);
	}

	@Override
	public void del(BankCollectDomain domain) {
		if (StringUtils.isEmpty(domain.getUrl()))
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getUrl()));
		bankCollectDao.delete(query, BankCollectDomain.class);
	}

	@Override
	public synchronized void update(BankCollectDomain domain) {
		if (StringUtils.isEmpty(domain.getUrl()))
			return;
		String _id = domain.getUrl();
		domain.setUrl(null);
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			bankCollectDao.update(Criteria.where("_id").is(_id), update);
		}
	}

	@Override
	public BankCollectDomain getById(String id) {
		if (StringUtils.isEmpty(id))
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BankCollectDomain baseBankDomain = bankCollectDao.findOne(query);
		return baseBankDomain;
	}

	@Override
	public List<BankCollectDomain> getList(BankCollectDomain domain) {
		Query query = parseDomaintoQuery(domain);
		List<BankCollectDomain> bankcollectList = bankCollectDao.findList(query);
		return bankcollectList;
	}

	@Override
	public List<BankCollectDomain> getListbyCriteria(Criteria criteria) {
		return bankCollectDao.findList(criteria);
	}

	@Override
	public Page<BankCollectDomain> getPageResult(BankCollectDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		// 存在的属性列表
		if (null != domain.getFieldExists()) {
			for (String prop : domain.getFieldExists()) {
				query.addCriteria(Criteria.where(prop).exists(true));
			}
		}
		// 不存在的属性列表
		if (null != domain.getFieldNoneExists()) {
			for (String prop : domain.getFieldNoneExists()) {
				query.addCriteria(Criteria.where(prop).exists(false));
			}
		}
		// date列表
		if (null != domain.getDateSearchMap()) {
			for (Entry<String, String> entry : domain.getDateSearchMap().entrySet()) {
				String[] values = entry.getValue().split(":");
				Date start = null;
				Date end = null;
				if (StringUtils.isNotEmpty(values[0])) {
					try {
						start = Globals.FORMAT_TYPE_E.parse(values[0]);
					} catch (ParseException e) {
					}
				}
				if (StringUtils.isNotEmpty(values[1])) {
					try {
						end = Globals.FORMAT_TYPE_E.parse(values[1]);
					} catch (ParseException e) {
					}
				}
				if (StringUtils.isNotEmpty(values[0]) && StringUtils.isNotEmpty(values[1])) {
					query.addCriteria(Criteria.where(entry.getKey()).gte(start).lte(end));
				} else if (StringUtils.isNotEmpty(values[0]) && StringUtils.isEmpty(values[1])) {
					query.addCriteria(Criteria.where(entry.getKey()).gte(start));
				} else if (StringUtils.isEmpty(values[0]) && StringUtils.isNotEmpty(values[1])) {
					query.addCriteria(Criteria.where(entry.getKey()).lte(end));
				}
			}
		}
		Page<BankCollectDomain> domainPage = bankCollectDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public synchronized void update(BankCollectDomain domain, BankCollectDomain condition) {
		Query query = parseDomaintoQuery(condition);
		Update update = parseDomaintoUpdate2(domain);

		if (update.getUpdateObject().toMap().size() != 0) {
			bankCollectDao.update(query, update);
		}
	}

	@Override
	public List<BankCollectDomain> findAll() {
		return bankCollectDao.findAll();
	}

	@Override
	public void saveOrUpdate(BankCollectDomain domain) {
		if (null == domain) {
			return;
		}
		BankCollectDomain paraDomain = new BankCollectDomain();
		paraDomain.setUrl(domain.getUrl());
		BankCollectDomain retAct = bankCollectDao.findOne(parseDomaintoQuery(paraDomain));

		if (null != retAct) {
			paraDomain.setUrl(retAct.getUrl());
			update(domain, paraDomain);
		} else {
			save(domain);
		}
	}

	@Override
	public List<BankCollectDomain> getListByIds(List<Long> ids, BaseQueryOrder order) {
		if (null != ids && ids.size() > 0) {
			Query qry = new Query();
			qry.addCriteria(Criteria.where("_id").in(ids));
			return bankCollectDao.getPageResult(qry, order).getContent();
		}
		return Lists.newArrayList();
	}
}
